package com.bw.shop_2302.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.base.entity.LoginCodeEntity
import com.bw.base.entity.Res
import com.bw.base.entity.SendCodeEntity
import com.bw.base.mvvm.BaseViewModel
import com.bw.shop_2302.utils.SingleLiveData
import kotlinx.coroutines.launch

class LoginViewModel:BaseViewModel() {

    val sendsuccess= SingleLiveData<Res<SendCodeEntity>>()
    var sendfail=SingleLiveData<String>()

    fun sendcode(phone:String){
        //在作用域中调用挂起函数,自己添加try-catch 防止有异常情况时项目崩溃
        viewModelScope.launch {
            try {
                //直接调用Api中的
                sendsuccess.postValue(apiServer.SendCode(phone))
            }catch (e:Exception){
                sendfail.postValue(e.message.toString())
            }

        }
    }

    //验证码登录

    val LoginCodesuccess=SingleLiveData<Res<LoginCodeEntity>>()
    var LoginCodefail=SingleLiveData<String>()

    fun LoginCode(captcha: String,phone:String){
        //在作用域中调用挂起函数,自己添加try-catch 防止有异常情况时项目崩溃
        viewModelScope.launch {

            try {
                val map= mutableMapOf<String,Any>()
                map["captcha"]=captcha
                map["phone"]=phone
                LoginCodesuccess.postValue(apiServer.LoginCode(mapToBody(map)))

            }catch (e:Exception){

                LoginCodefail.postValue(e.message.toString())

            }

        }
    }


}